<?php

namespace app\api\model;

use app\service\im\Group as ServiceGroup;
use app\service\im\Group;
use think\Model;

class RtcAnchor extends Model
{
    //开启自动完成时间戳功能
    protected $autoWriteTimestamp = true;
    // 定义时间戳字段名
    protected $createTime = 'create_time';
    protected $updateTime = 'update_time';


    /**
     * Notes:创建房间和加入房间成员(群主)
     * User: tangfen
     * Date: 2023/12/27
     * Time: 14:51
     * @param $params
     */
    public function insertRtcAnchor($data)
    {
        $this->data($data);
        $result = $this->save();
        if ($result) {
            $rtcPlay = new RtcPlay();
            $data['rtc_anchor_id'] = $this->id;
            //加入频道成员(群主)
            return $rtcPlay->insertRtcPlay($data, 1);//群主也加入成员表
        } else {
            return_ajax(0, '创建频道入库失败');
        }
    }

    /**
     * Notes:编辑房间
     * User: tangfen
     * Date: 2023/12/28
     * Time: 16:26
     * @param $data
     * @return bool
     */
    public function editRtcAnchor($data)
    {
        if (!empty($data['homeowner_id'])) {//新房主uid
            //更新im群表群主
            $group_id = $this::where('id', $data['id'])->value('im_group_id');
            $homeownerId = ImGroup::where('id', $group_id)->update(['uid' => $data['homeowner_id']]);//更换im群房主

            $rtcAnchor1 = $this::where('id', $data['id'])->update(['uid' => $data['homeowner_id']]);//更新房间群主
            $rtcAnchor2 = RtcPlay::where('rtc_anchor_id', $data['id'])->update(['is_homeowner' => 2]);//全部房间改为否
            $rtcAnchor3 = RtcPlay::where(['rtc_anchor_id' => $data['id'], 'uid' => $data['homeowner_id']])->update(['is_homeowner' => 1]);//重新设置房间群主

            if ($homeownerId ===false || $rtcAnchor1 === false || $rtcAnchor2 ===false || $rtcAnchor3 ===false) {
                return_ajax(0, '更新失败1');
            }

            //转让腾讯im群主
            $res = new ServiceGroup();
            $groupId = ImGroup::where('id', $group_id)->value('GroupId');
//            var_dump($groupId);
//            var_dump($data['homeowner_id']);die;
            $data = $res->changeGroupOwner($groupId, $data['homeowner_id']);
            if ($data['ActionStatus'] == 'OK') {
                return true;
            } else {
                return_ajax(0, '转让腾讯im群主失败',$data);
            }

        }

        $result = $this::where('id', $data['id'])->update(['name' => $data['name'], 'is_lock' => $data['is_lock'], 'status' => $data['status'], 'is_automatic' => $data['is_automatic']]);
        if ($result !== false) {
            return true;
        } else {
            return_ajax(0, '编辑失败');
        }
    }

    /**
     * Notes:获取房间列表
     * User: tangfen
     * Date: 2024/1/3
     * Time: 13:07
     * @param $params
     * @return RtcAnchor|\think\Paginator
     */
    public function getRtcList($params)
    {
        $where = [];
        $where[] = ['ra.status', '=', 2];//未解散1=是，2=否
        if (!empty($params['is_lock']) && isset($params['is_lock'])) {
            $where[] = ['ra.is_lock', '=', $params['is_lock']];
        }
        if (!empty($params['channel_id']) && isset($params['channel_id'])) {
            $where[] = ['ra.channel_id', 'like', '%' . $params['channel_id'] . '%'];
        }
        if (!empty($params['name']) && isset($params['name'])) {
            $where[] = ['ra.name', 'like', '%' . $params['name'] . '%'];
        }

        $list = $this::alias('ra')
            ->join('user u', 'u.id=ra.uid', 'LEFT')
            ->join('im_game_category gc', 'gc.id=ra.game_category_id', 'LEFT')
            ->where($where)
            ->field('ra.*, u.nick,u.portrait,u.username,u.mobile,gc.category_name')
            ->order('ra.create_time asc')
            ->paginate($params['limit'], false, ['page' => $params['page']]);

        return $list;
    }

    /**
     * Notes:获取房间详情
     * User: tangfen
     * Date: 2024/1/3
     * Time: 13:40
     */
    public function channelDetails($params)
    {
        $where[] = ['ra.channel_id', '=', $params['channel_id']];
        $res = $this::alias('ra')
            ->join('user u', 'u.id=ra.uid', 'LEFT')
            ->join('im_game_category gc', 'gc.id=ra.game_category_id', 'LEFT')
            ->where($where)
            ->field('ra.*, u.nick,u.portrait,u.username,u.mobile,gc.category_name')
            ->find();
        return $res;
    }

    //获取腾讯im群GroupId
    public function getGroupId($channel_id)
    {
        $where = [
            'ra.channel_id' => $channel_id,
        ];
        $info = $this::alias('ra')
            ->join('im_group ig', 'ig.id=ra.im_group_id', 'LEFT')
            ->where($where)
            ->value('ig.GroupId');
        return $info;
    }

    /**
     * Notes:解散房间
     * User: tangfen
     * Date: 2023/12/28
     * Time: 15:56
     * @param $channel_id
     * @return string
     */
    public function deleteGroup($channel_id)
    {
        $groupId = $this->getGroupId($channel_id); //获取腾讯群id
        $res = new Group();
        $data = $res->destroyGroupMember($groupId);

        if ($data['ActionStatus'] == 'OK') { //ErrorCode = 10010   this group does not exist
            return 'true';
        } else {
            if ($data['ErrorCode'] == 10010) {
                return_ajax(0, 'this group does not exist');
            }
            return_ajax(0, '删除腾讯第三方群失败');
        }
    }

    /**
     * Notes:踢出房间
     * User: tangfen
     * Date: 2023/12/28
     * Time: 15:56
     */
    public function RemoveGroup($channel_id, $Member_Account)
    {
        $groupId = $this->getGroupId($channel_id); //获取腾讯群id
        $res = new Group();
        $data = $res->delGroupMember($groupId, $Member_Account);//删除腾讯群用户
        if ($data['ActionStatus'] == 'OK') {
            return 'true';
        } else {
            return_ajax(0, '删除腾讯群用户失败', $data);
        }
    }

    /**
     * Notes:群主禁麦
     * User: tangfen
     * Date: 2023/12/28
     * Time: 19:56
     */
    public function prohibitWheat($data)
    {
        $res = $this::where('channel_id', $data['channel_id'])->update(['is_prohibit_wheat' => $data['is_prohibit_wheat']]);
        if ($res !== false) {
            return 'true';
        } else {
            return false;
        }
    }

    /**
     * Notes:群主控麦
     * User: tangfen
     * Date: 2023/12/28
     * Time: 19:56
     */
    public function controlWheat($data)
    {
        $res = $this::where('channel_id', $data['channel_id'])->update(['is_control_wheat' => $data['is_control_wheat']]);
        if ($res !== false) {
            return 'true';
        } else {
            return false;
        }
    }

    /**
     * Notes:是否为房主
     * User: tangfen
     * Date: 2023/12/28
     * Time: 20:21
     */
    public function isHomeowner($data)
    {
        $uid = $this::where('channel_id', $data['channel_id'])->value('uid');
        if ($uid == $data['uid']) {
            return 1;//是房主
        } else {
            return 2;//不是
        }
    }

    /**
     * Notes:判断抢麦用户是否可以发言(说话)
     * User: tangfen
     * Date: 2023/12/28
     * Time: 20:21
     */
    public function isCanTalk($data)
    {
        $res = RtcWheat::where(['channel_id' => $data['channel_id'], 'status' => 1])->order('create_time asc')->field('uid,wheat_end_time')->find();//房间排第一的用户信息

        if ($res['uid'] == $data['uid']) {
            return 1;//可以发言
        } else {
            return 2;//不可以发言
        }
    }


}